package QQclient;

import QQcommon.Massage_2;
import QQcommon.Message;
import QQcommon.User_qq;
import QQcommon.Utility;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Date;


public class UserclientService {
    User_qq user=new User_qq();
    boolean b=false;
    private Socket socket;

    public UserclientService(){}
    public boolean checkUser(String UserId,String pwd){
           user.setUserId(UserId);
           user.setPwd(pwd);
        try {
            socket=new Socket(InetAddress.getLocalHost(), Integer.parseInt("9999"));
            ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(user);//发送user对象

            ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
            Message mes=(Message) ois.readObject();//接收服务端发回的信息

            if(mes.getMassageType().equals(Massage_2.Massage_LogIn_success)){//登录成功
                  ClientThread clientThread=new ClientThread(socket);
                  clientThread.start();
                  Thread_Hashmap.Add_Tread(UserId,clientThread);//加入集合中
                  b=true;
            }else {
                  socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }
    //向服务端请求在线用户列表
    public void onlineFriendList(){
        Message message=new Message();
        message.setMassageType(Massage_2.MESSAGE_GET_ONLINE_FRIEND);
        message.setSender(user.getUserId());

        try {
            //通过Socket得到对应的ObjectOutputStreamd对象
            ObjectOutputStream oos=new ObjectOutputStream(Thread_Hashmap.Get_Client(user.getUserId()).getSocket().getOutputStream()) ;
            oos.writeObject(message);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //向服务端请求退出线程
    public void ExitThread(){
        Message message=new Message();
        message.setMassageType(Massage_2.MESSAGE_CLIENT_EXIT);
        message.setSender(user.getUserId());
        try {
            //通过Socket得到对应的ObjectOutputStreamd对象
            ObjectOutputStream oos=new ObjectOutputStream(Thread_Hashmap.Get_Client(user.getUserId()).getSocket().getOutputStream()) ;
            oos.writeObject(message);

            System.exit(0);
            System.out.println(user+"退出程序");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void Chat(String getID,String chat){
        Message message=new Message();
        message.setMassageType(Massage_2.MESSAGE_COMM_MES);
        message.setSender(user.getUserId());
        message.setGetter(getID);
        message.setContent(chat);
        message.setSendTime(new Date().toString());
        System.out.println(message.getSender()+"对"+message.getGetter()+"说"+message.getContent());
        try {
            //通过Socket得到对应的ObjectOutputStreamd对象
            ObjectOutputStream oos=new ObjectOutputStream(Thread_Hashmap.Get_Client(user.getUserId()).getSocket().getOutputStream()) ;
            oos.writeObject(message);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void Chats(String chats){
        Message message=new Message();
        message.setMassageType(Massage_2.MESSAGE_TO_ALL_MES);
        message.setSender(user.getUserId());
        message.setContent(chats);
        message.setSendTime(new Date().toString());
        System.out.println(message.getSender()+" 对大家说 "+message.getContent());
        try {
            //通过Socket得到对应的ObjectOutputStreamd对象
            ObjectOutputStream oos=new ObjectOutputStream(Thread_Hashmap.Get_Client(user.getUserId()).getSocket().getOutputStream()) ;
            oos.writeObject(message);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void File(String getId,String src,String dest){
        Message message=new Message();
        message.setMassageType(Massage_2.MESSAGE_SEND_FILE);
        message.setSender(user.getUserId());
        message.setGetter(getId);
        message.setSrc(src);
        message.setDest(dest);

        //读取文件
        FileInputStream fileInputStream=null;
        byte[] filebytes=new byte[(int) new File(src).length()];
        try {
            fileInputStream=new FileInputStream(src);
            fileInputStream.read(filebytes);//将文件读入字节数组

            //读入到message中去
            message.setFileBytes(filebytes);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                ObjectOutputStream oos=new ObjectOutputStream(Thread_Hashmap.Get_Client(user.getUserId()).getSocket().getOutputStream()) ;
                oos.writeObject(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

